
const { colletlist, cancalUsercollet, findCollet, findMyfavoriteCollect } = require('../services/UserlistService');


//音乐keen控制器
class UserLikeController {

    //收藏歌单
    async collectKeen(ctx, next) {
        try {
            const { user_id } = ctx.state.user
            const { id, coverImgUrl, name } = ctx.request.body
            const newObj = Object.assign({ id, coverImgUrl, name }, { user_id })

            const result = await colletlist(newObj)
            ctx.body = {
                code: "0",
                message: "success",
                result
            }
        } catch (error) {
            console.log(error);

        }
    }

    //取消喜欢
    async cancelCollect(ctx, next) {
        const { user_id } = ctx.state.user
        const { id } = ctx.request.body
        const res = await cancalUsercollet({ user_id, id })
        if (res) {
            ctx.body = {
                code: '0',
                message: "success",
                result: '取消成功'
            }
        }
    }

    //数据处理
    async musicSearchcollect(ctx, next) {

        try {
            const { user_id } = ctx.state.user
            const myMusicArr = await findCollet(user_id)
            console.log(ctx.request.body.data, 123);
            const SearchArr = ctx.request.body.data
            const mySet = new Set(myMusicArr.map(item => item.id))
            // console.log(mySet);
            // console.log(SearchArr);
            // console.log(myMusicArr == "");
            if (myMusicArr == "") {
                const res = {
                    ...SearchArr, "isFavorite": 0
                }
                return ctx.body = {
                    res
                }
            }
            const res = {
                ...SearchArr, "isFavorite": mySet.has(SearchArr.id) ? 1 : 0
            }

            ctx.body = {
                res
            }
        } catch (error) {
            console.log(error);

        }
    }

    //查询我喜欢的音乐
    async musicCollect(ctx, next) {
        try {
            const { user_id } = ctx.state.user
            const { pageNo, pageSize } = ctx.request.query
            const result = await findMyfavoriteCollect({ user_id, pageNo, pageSize })
            ctx.body = {
                code: '0',
                message: 'success',
                result
            }
        } catch (error) {
            console.log(error);

        }


    }
}

module.exports = new UserLikeController()